import React, { Component } from 'react';

import './index.css'

class Item extends Component {

    state = {
        mouse: false
    }

    // 获取鼠标移入移出，
    handleMouse = (flag) => {
        // 因为调用时间加了括号传值，我们需要使用高阶函数，也就是在套用一层，
        // 否则初始化会直接输出
        return () => {
            this.setState({mouse:flag})
        }
    }

    handleChecked = (id) => {
        return (event) => {
            this.props.updateTodo(id,event.target.checked)
        }
    }

    handleDelete = (id) => {
        if(window.confirm('您确定删除吗？')) {
            this.props.handleDelete(id)
        }
    }

    render() {
        const {id,name,done} = this.props
        const {mouse} = this.state
        return (
            <li style={{backgroundColor: mouse?'#ddd': 'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleChecked(id)}/>
                    <span>{name}</span>
                </label>
                <button onClick={()=> this.handleDelete(id)} className="btn btn-danger" style={{display: mouse?'block': 'none'}}>删除</button>
            </li>
        );
    }
}

export default Item;